/**
   CS 305 & 503, Data Structures and Algorithms
   Programming Assignment 1, Fall 2010

   A linked-sequence action represents an operation on a linked-sequence ADT.
   An action indicates the operation performed, its input arguments (if any),
   its resulting value (if any), and whether or not the operation was
   successful.

   For example, successfully pushing the value v on a stack would be the action

     operation: put
     index    : 0
     value    : v
     return   : undefined
     success  : true

   and the action 

     operation: see
     index    : 2
     value    : undefined
     return   : undefined
     success  : false

   represents unsuccessfully returning the third value from a linked list with
   less than three values.

   A linked-sequence action is parametric in the type of the value stored in
   the linked-sequence ADT.
 */


class
LinkedSequenceAction<E> {

  /**
     The operations that can be performed on a linked-sequence ADT.
   */

  public enum 
  Operation {
    end,
    get,
    put,
    see,
    };


  /**
     The linked-sequence operation performed by this action.
   */

  final public Operation operation;


  /**
     The index used as input to the operation performed by this action.  It
     contains an undefined value if the operation performed by this action
     doesn't need an index.
   */

  final public int index;


  /**
     The value used as input to the operation performed by this action.  It
     contains an undefined value if the operation performed by this action
     doesn't need a value.
   */

  final public E value;


  /**
     True if the operation performed by this action was successful; false if
     the operation failed for any reasion.
   */

  final public boolean success;


  /**
     The value returned by the operation performed by this action.  result
     contains an undefined value if the operation doesn't return a value, or if
     the operation was unccessful.
   */

  final public E result;


  /**
     Create a new linked-sequence action.

     @param o The operation being performed.

     @param i The index given to the operation being performed (ignored if the
     operation doesn't require an index).

     @param v The value given to the operation being performed (ignored if the
     operation doesn't require a value).

     @param s True if the given operation with the given index and value (if
     required) completed successfully; false otherwise.

     @param r The value returned by the operation, if it as successful; ignored
     if the operation was uncessful.
   */

  public LinkedSequenceAction(Operation o, Integer i, E v, boolean s, E r) {

    assert o != null : "linked-sequence action operation is null";

    indexObject = i;

    operation = o;
    index = i == null ? -1 : i.intValue();
    value = v;
    success = s;
    result = r;
    }


  @Override
  public String
  toString() {
    return 
      (new StringBuilder("("))
	.append(operation)
	.append(", ")
	.append(indexObject == null ? "undefined" : index)
	.append(", ")
	.append(value == null ? "undefined" : value)
	.append(", ")
	.append(success)
	.append(", ")
	.append(result == null ? "undefined" : result)
	.append(")")
	.toString();
    }


  /**
     This action's index value or null if this action doesn't need an index.
   */

  private final Integer indexObject;
  }


// $Log: LinkedSequenceAction.java,v $
// Revision 1.3  2010/10/05 05:05:35  rclayton
// Explode when constructing an action with a null operation; make the
// undefined index value negative.
//
// Revision 1.2  2010/09/30 16:57:38  rclayton
// Override toString().
//
// Revision 1.1  2010/09/29 13:04:55  rclayton
// Initial revision
//
